home *** CD-ROM | disk | FTP | other *** search
/ Best of Shareware / Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso / mac / DOS / CAD_CAM / A7221V1B / COMIO.H < prev    next >
C/C++ Source or Header  |  1992-03-12  |  8KB  |  223 lines

  1. /*
  2.    Module: comio.h
  3.    Date:   3/9/92
  4.    Version: 1.0b
  5.    Author:  Dave Lutz
  6.    Email:   lutz@psych.rochester.edu
  7.    Copyright: 1992 University of Rochester, Psychology Dept.
  8.  
  9.    Disclaimer:  This software is distributed free of charge.  As such, it
  10.                 comes with ABSOLUTELY NO WARRANTY.  The user of the software
  11.                 assumes ALL RISKS associated with its use.
  12.  
  13.                 Your rights to modify and/or distribute this software are
  14.                 outlined in the file SEND7221.DOC.
  15.  
  16.    Purpose: This module provides the codes and function prototypes needed to
  17.             use the comio input/output functions.  The comio functions use
  18.             dos interrupts to initiate i/o operations on the com port of an
  19.             IBM compatible PC.
  20.  
  21.             The COMPORT structure is also defined here.  This structure
  22.             is used by all of the comio functions.
  23. */
  24.  
  25. #include <time.h>
  26. #include <dos.h>
  27.  
  28. #define COMPORT struct serial_port
  29.  
  30. COMPORT {
  31.    union REGS inregs,
  32.               outregs;
  33.    time_t timeout;
  34. };
  35.  
  36. /* pass these codes to any function that requires a com port designator */
  37. #define COM1      0
  38. #define COM2      1
  39. #define COM3      2
  40. #define COM4      3
  41.  
  42. /*  The following codes are used to set: number of data bits, number of stop
  43.     bits, parity, and baud rate.  You should select one of each, combine them
  44.     with a bitwise or (|), and pass them to any function that requires a
  45.     configuration paramter.
  46.  
  47.     For example, the configuration for a serial port with 7 data bits, 1 stop
  48.     bit, even parity, and 2400 baud would be specified as follows:
  49.  
  50.        (DATA_7 | STOP_1 | PAR_E | BAUD_2400)
  51. */
  52.  
  53. /* number of data bits: 7 bits, 8 bits, don't care */
  54. #define DATA_7     0x02
  55. #define DATA_8     0x03
  56. #define DATA_D     0x00
  57.  
  58. /* number of stop bits */
  59. #define STOP_1     0x00
  60. #define STOP_2     0x04
  61.  
  62. /* parity: none, odd, even, don't care */
  63. #define PAR_N      0x00
  64. #define PAR_O      0x08
  65. #define PAR_E      0x18
  66. #define PAR_D      0x10
  67.  
  68. /* baud rate */
  69. #define BAUD_110   0x00
  70. #define BAUD_150   0x20
  71. #define BAUD_300   0x40
  72. #define BAUD_600   0x60
  73. #define BAUD_1200  0x80
  74. #define BAUD_2400  0xA0
  75. #define BAUD_4800  0xC0
  76. #define BAUD_9600  0xE0
  77.  
  78.  
  79.  
  80. /*
  81.    The following codes can be used to determine the Line Control Status and
  82.    Modem Control Status of the COMPORT.  Combine them with the returned
  83.    status value with a bitwise and (&), and if the result is non-zero that 
  84.    flag has been set.
  85.  
  86.    Line Control Statys flags begin with LCS.
  87.    Modem Control Status flags begin with MCS.
  88. */
  89.  
  90. #define LCS_RDR  0x0100    /* received data ready */
  91. #define LCS_OVRN 0x0200    /* data overrun */
  92. #define LCS_PAR  0x0400    /* parity error */
  93. #define LCS_FRM  0x0800    /* framing error - incorrect start/stop bit */
  94. #define LCS_BRK  0x1000    /* break detect */
  95. #define LCS_TRH  0x2000    /* Transmitter hold empty - ready for new send */
  96. #define LCS_TRS  0x4000    /* Transmitter shift empty - not transmitting */
  97. #define LCS_TIM  0x8000    /* Timeout error */
  98.  
  99. #define MCS_CSCHNG 0x0001  /* Clear To Send (CTS) changed state */
  100. #define MCS_DRCHNG 0x0002  /* Data Set Ready (DSR) changed state */
  101. #define MCS_ENDRNG 0x0004  /* End of ringing pulse detector */
  102. #define MCS_CDCHNG 0x0008  /* Carrier Detect (CD) changed state */
  103. #define MCS_CTS    0x0010  /* Clear To Send status */
  104. #define MCS_DSR    0x0020  /* Data Set Ready status */
  105. #define MCS_RING   0x0040  /* Ringing indicator */
  106. #define MCS_CD     0x0080  /* Carrier Detect Status */
  107.  
  108.  
  109.  
  110. /*
  111.    Function: cominit
  112.    Purpose:  set up a COMPORT for subsequent io operations
  113.  
  114.    Pre: cpp is a pointer to a COMPORT data structure.
  115.         portdes is one of the port designators defined in comio.h
  116.         config is the desired configuration for the com port, and was
  117.         obtained by combining the configuration constants defined in
  118.         comio.h with a bitwise or.
  119.         timeout is the number of seconds to wait for the first character
  120.         during an input operation.
  121.  
  122.    Post: The portdes, config, and timeout paramaters are transferred to the 
  123.          cpp structure.
  124.          A dos interrupt is called to open and configure the desired
  125.          serial port.
  126.          The bitmapped status of the port is returned.
  127.  
  128.          The status of the port can be determined by comparing it to the
  129.          LCS_* and MCS_* values defined in comio.h.  For example:
  130.  
  131.                 (MCS_DSRSTAT & retval) 
  132.          
  133.          will be nonzero if Modem Control Status indicates Data Set Ready.
  134. */
  135. unsigned cominit (COMPORT *cpp, int portdes, unsigned config, time_t timeout);
  136.  
  137.  
  138.  
  139. /*
  140.    Function: comin
  141.    Purpose:  read a block of characters from the serial port.
  142.  
  143.    Pre: cpp is a pointer to a COMPORT structure.
  144.         cpp has been prepared via a call to cominit.
  145.         Nobody has been mucking around with the internals of the
  146.         cpp structure...
  147.         buff is a pointer to storage for the bytes read from the
  148.         port.  There is enough storage for max characters.
  149.         max is the maximum number of chars to read.
  150.         term is a character which, when read, signals the end of
  151.         the input stream.
  152.         If there is no real term character, term = -1.
  153.  
  154.    Post:  Characters are read from the serial port and stored in buff
  155.           until: max chars have been read, the term char is read, or an
  156.           I/O error occurs.
  157.           The function will wait the predefined (by cominit) number of 
  158.           seconds for the first character to appear.
  159.           After the first character appears, the remaining chars are
  160.           expected to follow immediately.  If they don't, the function
  161.           will exit.
  162.           The number of characters actually read is returned.
  163.           The serial port should be checked with comlstat to determine
  164.           if an error has occurred.
  165. */
  166. int comin (COMPORT *cpp, char *buff, int max, int term);
  167.  
  168.  
  169.  
  170. /*
  171.    Function: comout
  172.    Purpose:  Send a block of characters through the serial port.
  173.  
  174.    Pre: cpp is a pointer to a COMPORT structure.
  175.         cpp has been prepared via a call to cominit.
  176.         buff is a pointer to the block of chars to be transmitted.
  177.         buffcnt is the number of chars to send.
  178.  
  179.    Post: An attempt is made to send the block of chars through the
  180.          serial port.
  181.          The number of chars actually sent is returned.
  182.          If all bytes were not sent, it should be possible to determine
  183.          the reason by making a call to comlstat.
  184. */
  185. int comout (COMPORT *cpp, char *buff, int buffcnt);
  186.  
  187.  
  188.  
  189. /*
  190.    Function: comstat
  191.    Purpose:  Report the current status of the com port.
  192.  
  193.    Pre: cpp is a pointer to a COMPORT structure.
  194.         cpp has been set up via a call to comopen.
  195.         Nobody has been mucking around with the internals of the
  196.         cpp structure...
  197.  
  198.    Post:  A DOS interrupt is called to check the status of the port
  199.           specified in *cpp.
  200.           The status of the port is returned as a bitmapped value.
  201.           This value can be interpreted by comparing it to the LCS_*
  202.           and MCS_* masks defined in comio.h
  203. */
  204. unsigned comstat (COMPORT *cpp);
  205.  
  206.  
  207. /*
  208.    Function: comlstat
  209.    Purpose:  Report the status of the com port as it was after the last
  210.              call to one of the other comio functions.
  211.  
  212.    Pre: cpp is a pointer to a COMPORT stucture.
  213.         cpp has been set up via a call to comopen.
  214.         Nobody has been mucking around with the internals of the
  215.         cpp structure...
  216.  
  217.    Post: The last status of the com port is returned as a bitmapped
  218.          value.
  219.          This value can be interpreted by comparing it to the LCS_*
  220.          and MCS_* masks defined in comio.h
  221. */
  222. unsigned comlstat (COMPORT *cpp);
  223.